package org.west.sky.frame.activiti.study.chapter15;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * author: chz
 * date: 2025/4/15
 * description: Shell任务
 */
@Slf4j
public class RunDemo15_5 {

    @Test
    public void run() {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("activiti.spring.xml");

        ProcessEngineConfiguration processEngineConfiguration = (ProcessEngineConfiguration) applicationContext.getBean("processEngineConfiguration");
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/chapter15/Chapter15_5.bpmn20.xml").deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();

        Map<String, Object> variables = new HashMap<>();
        variables.put("browserLocation", "C:\\Users\\DELL\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
        variables.put("webUrl", "https://www.epubit.com");
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====完成任务：" + task.getName());
        taskService.complete(task.getId(), variables);

        processEngine.close();
    }
}
